home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / ODF Release 3 / ODFDev / Container / Sources / Part.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  11.0 KB  |  397 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Part.cpp
  4. //    Release Version:    $ ODF 3 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "Container.hpp"
  11.  
  12. #ifndef PART_H
  13. #include "Part.h"
  14. #endif
  15.  
  16. #ifndef BINDING_K
  17. #include "Binding.k"
  18. #endif
  19.  
  20. #ifndef DEFINES_K
  21. #include "Defines.k"
  22. #endif
  23.  
  24. #ifndef CONTENT_H
  25. #include "Content.h"
  26. #endif
  27.  
  28. #ifndef FRAME_H
  29. #include "Frame.h"
  30. #endif
  31.  
  32. #ifndef SELECT_H
  33. #include "Select.h"
  34. #endif
  35.  
  36. #ifndef COMMANDS_H
  37. #include "Commands.h"
  38. #endif
  39.  
  40. #ifndef PROXY_H
  41. #include "Proxy.h"
  42. #endif
  43.  
  44. #ifndef VIEW_H
  45. #include "View.h"
  46. #endif
  47.  
  48. // ----- Part Layer -----
  49.  
  50. #ifndef FWABOUT_H
  51. #include "FWAbout.h"
  52. #endif
  53.  
  54. #ifndef FWPRESEN_H
  55. #include "FWPresen.h"
  56. #endif
  57.  
  58. #ifndef FWUTIL_H
  59. #include "FWUtil.h"
  60. #endif
  61.  
  62. #ifndef FWPRTITE_H
  63. #include "FWPrtIte.h"
  64. #endif
  65.  
  66. // ----- OS Layer -----
  67.  
  68. #ifndef FWCOLORP_H
  69. #include "FWColorP.h"
  70. #endif
  71.  
  72. #ifndef FWRECT_H
  73. #include "FWRect.h"
  74. #endif
  75.  
  76. #ifndef FWMENU_H
  77. #include "FWMenu.h"
  78. #endif
  79.  
  80. #ifndef FWCFMRES_H
  81. #include "FWCFMRes.h"
  82. #endif
  83.  
  84. #ifndef SLMIXOS_H
  85. #include "SLMixOS.h"
  86. #endif
  87.  
  88. #ifndef SLMIXOS_H
  89. #include "SLMixOS.h"
  90. #endif
  91.  
  92. #ifndef FWODGEOM_H
  93. #include "FWODGeom.h"
  94. #endif
  95.  
  96. #ifndef FWTRCTCL
  97. #include "FWTRctCl.h"
  98. #endif
  99.  
  100. // ----- ColorExtension Includes -----
  101.  
  102. #ifndef CESERVER_H
  103. #include "CEServer.h"
  104. #endif
  105.  
  106. // ----- OpenDoc Includes -----
  107.  
  108. #ifndef SOM_ODSession_xh
  109. #include <ODSessn.xh>
  110. #endif
  111.  
  112. #ifndef SOM_Module_OpenDoc_StdProps_defined
  113. #include <StdProps.xh>
  114. #endif
  115.  
  116. //========================================================================================
  117. // Runtime Information
  118. //========================================================================================
  119.  
  120. #ifdef FW_BUILD_MAC
  121. #pragma segment odfcontainer
  122. #endif
  123.  
  124. //========================================================================================
  125. //    Globals
  126. //========================================================================================
  127.  
  128. FW_DEFINE_CLASS_M1(CContainerPart, FW_CEmbeddingPart)
  129. FW_DEFINE_AUTO(CContainerPart)
  130.  
  131. //========================================================================================
  132. //    class CContainerPart
  133. //========================================================================================
  134.  
  135. //----------------------------------------------------------------------------------------
  136. //    CContainerPart::CContainerPart
  137. //----------------------------------------------------------------------------------------
  138.  
  139. CContainerPart::CContainerPart(ODPart* odPart) :
  140.     FW_CEmbeddingPart(odPart, FW_gInstance, kPartInfoID),
  141.     fPartContent(NULL),
  142.     fMainPresentation(NULL)
  143. {
  144.     FW_END_CONSTRUCTOR
  145. }
  146.  
  147. //----------------------------------------------------------------------------------------
  148. //    CContainerPart::~CContainerPart
  149. //----------------------------------------------------------------------------------------
  150.  
  151. CContainerPart::~CContainerPart()
  152. {
  153.     FW_START_DESTRUCTOR
  154. }
  155.  
  156. //----------------------------------------------------------------------------------------
  157. //    CContainerPart::Initialize
  158. //----------------------------------------------------------------------------------------
  159.  
  160. void CContainerPart::Initialize(Environment* ev, ODStorageUnit* storageUnit, FW_Boolean fromStorage)
  161. {
  162.     // ----- Call Inherited Initialize -----
  163.     FW_CEmbeddingPart::Initialize(ev, storageUnit, fromStorage);
  164.  
  165.     fDrawingSize.Set(FW_IntToFixed(kDrawingSizeX), FW_IntToFixed(kDrawingSizeY));
  166.  
  167.     // We instantiate a submenu item from the stream so we must
  168.     // prevent it from being deadstripped.
  169.     FW_DO_NOT_DEAD_STRIP(FW_CSubMenuItem);
  170.     FW_DO_NOT_DEAD_STRIP(CContainerView);
  171.  
  172.     // ----- Tokenize -----
  173.     fContainerSelection = FW_NEW(CContainerSelection, (ev, this));    // Attention I don't own the selection the presentation will
  174.     
  175.     fMainPresentation = RegisterPresentation(ev, kMainPresentation, TRUE, kContainerView, kContainerView, fContainerSelection);
  176.     
  177.     CE_SetCallbacks(CContainerPart_SetColor, CContainerPart_SetColor);
  178.     CE_RegisterExtension(ev, this, this);
  179. }
  180.  
  181. //----------------------------------------------------------------------------------------
  182. //    CContainerPart::NewPartContent
  183. //----------------------------------------------------------------------------------------
  184.  
  185. FW_CContent* CContainerPart::NewPartContent(Environment* ev)
  186. {
  187.     fPartContent = FW_NEW(CPartContent, (ev, this));
  188.     return fPartContent;
  189. }
  190.  
  191. //----------------------------------------------------------------------------------------
  192. //    CContainerPart::AddProxyToPart
  193. //----------------------------------------------------------------------------------------
  194.  
  195. void CContainerPart::AddProxyToPart(Environment* ev, CProxy* proxy)
  196. {
  197.     fPartContent->AddProxy(ev, proxy);
  198. }
  199.  
  200. //----------------------------------------------------------------------------------------
  201. //    CContainerPart::DetachProxy
  202. //----------------------------------------------------------------------------------------
  203. //    At the end the embedded frames associated with 'proxy' will be in limbo
  204.  
  205. void CContainerPart::DetachProxy(Environment* ev, CProxy* proxy)
  206. {
  207.     proxy->DetachEmbeddedFrames(ev);        // Set embedded frames to in limbo state
  208.     
  209.     // Do this last.  May be the last reference to proxy!
  210.     fPartContent->RemoveProxy(ev, proxy);
  211. }
  212.  
  213. //----------------------------------------------------------------------------------------
  214. //    CContainerPart::DeleteProxy
  215. //----------------------------------------------------------------------------------------
  216. //    At the end the embedded frames are completly gone
  217.  
  218. void CContainerPart::DeleteProxy(Environment* ev, CProxy* proxy)
  219. {
  220.     DetachProxy(ev, proxy);
  221. }
  222.  
  223. //----------------------------------------------------------------------------------------
  224. // CContainerPart::AttachProxy
  225. //----------------------------------------------------------------------------------------
  226. //    Put back (for example after Undo) a proxy that has been detached
  227.  
  228. void CContainerPart::AttachProxy(Environment* ev, CProxy* proxy)
  229. {
  230.     AddProxyToPart(ev, proxy);    
  231.     proxy->AttachEmbeddedFrames(ev);
  232. }
  233.  
  234. //----------------------------------------------------------------------------------------
  235. //    CContainerPart::DoAbout
  236. //----------------------------------------------------------------------------------------
  237.  
  238. FW_Handled CContainerPart::DoAbout(Environment* ev)
  239. {
  240.     ::FW_About(ev, this, kAbout);
  241.     return FW_kHandled;
  242. }
  243.  
  244. //----------------------------------------------------------------------------------------
  245. //    CContainerPart::DoMenu
  246. //----------------------------------------------------------------------------------------
  247.  
  248. FW_Handled CContainerPart::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent)
  249. {
  250.     FW_Handled result = FW_kHandled;
  251.     ODCommandID commandID = theMenuEvent.GetCommandID(ev);
  252.  
  253.     switch (commandID)
  254.     {
  255.         case cSetBackgroundColor:
  256.             {
  257.                 FW_CColorPicker picker;
  258.                 
  259.                 picker.SetColor(GetBackgroundColor());
  260.                 if (picker.PickNewColor())
  261.                 {
  262.                     CSetBackgroundColorCommand* cmd = FW_NEW(CSetBackgroundColorCommand, (ev, this, picker.GetColor()));
  263.                     cmd->Execute(ev);
  264.                 }                
  265.             }
  266.             break;
  267.             
  268.         // sibling frames
  269.         case cAddSiblingFrame:
  270.             fContainerSelection->AddSiblingFrame(ev);
  271.             break;
  272.         
  273.         default:
  274.             result = FW_kNotHandled;        
  275.     }
  276.     
  277.     return result;
  278. }
  279.  
  280. //----------------------------------------------------------------------------------------
  281. //    CContainerPart::NewFrame
  282. //----------------------------------------------------------------------------------------
  283.  
  284. FW_CFrame* CContainerPart::NewFrame(Environment* ev, 
  285.                                 ODFrame* odFrame, 
  286.                                 FW_CPresentation* presentation,
  287.                                 FW_Boolean fromStorage)
  288. {
  289. FW_UNUSED(fromStorage);
  290.     return FW_NEW(CContainerFrame, (ev, odFrame, presentation, this));
  291. }
  292.  
  293. //----------------------------------------------------------------------------------------
  294. //    CDrawPart::DoAdjustMenus
  295. //----------------------------------------------------------------------------------------
  296.  
  297. FW_Handled CContainerPart::DoAdjustMenus(Environment* ev, 
  298.                                         FW_CMenuBar* menuBar, 
  299.                                         FW_Boolean hasMenuFocus, 
  300.                                         FW_Boolean isRoot)
  301. {
  302. FW_UNUSED(ev);
  303. FW_UNUSED(isRoot);
  304.     if (hasMenuFocus)
  305.         menuBar->EnableCommand(ev, kODCommandSelectAll, fPartContent->CountProxies() > 0);
  306.  
  307.     menuBar->EnableCommand(ev, cSetBackgroundColor, TRUE);
  308.  
  309.     // sibling frames
  310.     Boolean isWriteable = !IsReadOnly(ev);
  311.     Boolean hasSelection = !fContainerSelection->IsEmpty(ev);
  312.     menuBar->EnableCommand (ev, cAddSiblingFrame, isWriteable && hasSelection);
  313.     
  314.     return FW_kNotHandled;
  315. }
  316.  
  317. //----------------------------------------------------------------------------------------
  318. // CContainerPart::WhichProxy
  319. //----------------------------------------------------------------------------------------
  320.  
  321. CProxy* CContainerPart::WhichProxy(Environment* ev, FW_CGraphicContext& gc, 
  322.                 const FW_CPoint& where, FW_Boolean hasToBeSelected) const
  323. {
  324.     CContentProxyIterator ite(fPartContent);
  325.     for (CProxy* proxy = ite.Last(); ite.IsNotComplete(); proxy = ite.Previous())
  326.     {
  327.         FW_Boolean test = (!hasToBeSelected) || (hasToBeSelected && proxy->IsSelected());
  328.         if (test && proxy->HitTest(ev, gc, where))
  329.             return proxy;    
  330.     }
  331.     
  332.     return NULL;
  333. }
  334.  
  335. //----------------------------------------------------------------------------------------
  336. // CContainerPart::EmbeddedFrameRemoved
  337. //----------------------------------------------------------------------------------------
  338.  
  339. void CContainerPart::EmbeddedFrameRemoved(Environment *ev, FW_MProxy* proxy)
  340. {
  341.     CProxy* theProxy = (CProxy*)proxy;
  342.     
  343.     FW_CAcquiredODShape updateShape = ::FW_NewODShape(ev);
  344.     theProxy->GetUpdateBox(ev, updateShape);    
  345.     fMainPresentation->Invalidate(ev, updateShape);
  346.     
  347.     fPartContent->RemoveProxy(ev, theProxy);
  348. }
  349.  
  350. //----------------------------------------------------------------------------------------
  351. // CContainerPart::SetBackgroundColor
  352. //----------------------------------------------------------------------------------------
  353.  
  354. void CContainerPart::SetBackgroundColor(Environment* ev, const FW_CColor& newColor)
  355. {
  356.     if (IsReadOnly(ev))
  357.     {
  358.         FW_Beep();
  359.     }
  360.     else
  361.     {
  362.         FW_CColor oldColor = fPartContent->GetBackgroundColor();
  363.         if (newColor != oldColor)
  364.         {
  365.             fPartContent->SetBackgroundColor(newColor);
  366.             
  367.             FW_CPartFrameIterator iter(ev, this);
  368.             for (CContainerFrame* frame = (CContainerFrame*)iter.First(ev); 
  369.                     iter.IsNotComplete(ev); frame = (CContainerFrame*)iter.Next(ev))
  370.             {
  371.                 frame->UpdateContainerProperties(ev);
  372.             }
  373.             
  374.             fMainPresentation->Invalidate(ev);
  375.         }
  376.     }
  377. }
  378.  
  379. //----------------------------------------------------------------------------------------
  380. //    CContainerPart::GetBackgroundColor
  381. //----------------------------------------------------------------------------------------
  382.  
  383. FW_CColor CContainerPart::GetBackgroundColor() const
  384. {
  385.     return fPartContent->GetBackgroundColor();
  386. }
  387.  
  388. //----------------------------------------------------------------------------------------
  389. // CContainerPart_SetColor
  390. //----------------------------------------------------------------------------------------
  391.  
  392. void CContainerPart_SetColor(Environment* ev, void* refCon, short red, short green, short blue)
  393. {
  394.     FW_CColor newColor(red, green, blue);
  395.     ((CContainerPart*)refCon)->SetBackgroundColor(ev, newColor);
  396. }
  397.